package org.spring.springboot.controller;


import org.json.JSONObject;
import org.spring.springboot.domain.User;
import org.spring.springboot.domain.LoginInfo;
import org.spring.springboot.service.IUserService;
import org.spring.springboot.service.ILoginInfoSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService svc;

    @Autowired
    private ILoginInfoSvc loginInfoSvc;

    @PostMapping("/reg")
    public Map<String, Object> RegUser(@RequestBody Map<String, Object> jsonMap, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String password = jsonObject.getString("password");
        String phoneNum = jsonObject.getString("phoneNum");
        String uname = jsonObject.getString("uname");

        String userid = "";
        String ret = "success";
        String errMsg = "";
        User preUser = svc.selectOne(phoneNum);
        if (preUser == null) {
            userid = UUID.randomUUID().toString();
            User user = new User();
            user.setUserid(userid);
            user.setPhoneNum(phoneNum);
            user.setUname(uname);
            user.setPassword(password);

            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            dateFormat.format(date);
            user.setRegDate(date);
            svc.save(user);
        }
        else
        {
            errMsg = "用户已经存在,电话号码已注册过";
            userid = preUser.getUserid();
            uname = preUser.getUname();
            ret = "failed";
        }

        LoginInfo info = new LoginInfo();
        info.setId(UUID.randomUUID().toString());
        info.setLoginDate(new Date());
        info.setLoginName(uname);
        info.setIpaddr(request.getRemoteAddr());
        if (ret.compareTo("success") == 0){
            info.setIsSuccess(1);
        }
        else{
            info.setIsSuccess(0);
        }
        loginInfoSvc.save(info);

        JSONObject retData = new JSONObject();
        retData.put("uid",userid);
        retData.put("name",uname);

        String tokenID = UUID.randomUUID().toString();
        retData.put("token",tokenID);

        JSONObject retObj = new JSONObject();
        retObj.put("ret",ret);
        retObj.put("data",retData);
        retObj.put("errMsg",errMsg);

        return retObj.toMap();
    }


    @PostMapping("/login")
    public Map<String, Object> Login(@RequestBody Map<String, Object> jsonMap, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String password = jsonObject.getString("password");
        String phoneNum = jsonObject.getString("phoneNum");

        String uname = "";
        String userid = "";
        String ret = "success";
        String errMsg = "";
        User preUser = svc.selectOne(phoneNum);
        if (preUser != null) {
            userid = preUser.getUserid();
            uname = preUser.getUname();
            if (preUser.getPassword().compareTo(password) != 0) {
                System.out.println("passowrd:" + password);
                ret = "failed";
                errMsg = "密码错误";
            }
        }
        else
        {
            errMsg = "用户不存在";
            ret = "failed";
        }

        LoginInfo info = new LoginInfo();
        info.setId(UUID.randomUUID().toString());
        info.setLoginDate(new Date());
        info.setLoginName(uname);
        info.setIpaddr(request.getRemoteAddr());
        if (ret.compareTo("success") == 0){
            info.setIsSuccess(1);
        }
        else{
            info.setIsSuccess(0);
        }
        loginInfoSvc.save(info);

        JSONObject retData = new JSONObject();
        String tokenID = UUID.randomUUID().toString();
        retData.put("token",tokenID);
        retData.put("uid",userid);
        retData.put("name",uname);

        JSONObject retObj = new JSONObject();
        retObj.put("ret",ret);
        retObj.put("data",retData);
        retObj.put("errMsg",errMsg);

        return retObj.toMap();
    }


    @PostMapping("/delete")
    public String delete(@RequestBody Map<String, Object> jsonMap) {
        JSONObject jsonObject = new JSONObject(jsonMap);
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        svc.delete(id);
        String ret = "success";
        return ret;
    }

    @PostMapping("/visiteLog")
    public String visiteLog(@RequestBody Map<String, Object> jsonMap, HttpServletRequest request) {
        String clientIp = request.getRemoteAddr();
        String testStr = request.getHeader("X-Forwarded-For");
        JSONObject jsonObject = new JSONObject(jsonMap);
        String ret = "success";

        LoginInfo info = new LoginInfo();
        info.setId(UUID.randomUUID().toString());
        info.setLoginDate(new Date());
        info.setLoginName("guest");
        info.setIpaddr(testStr);
        info.setIsSuccess(1);
        loginInfoSvc.save(info);
        return ret;
    }
}
